package cn.duli.xianer.service.impl;


import cn.duli.xianer.dao.UserLoginAuthsMapper;
import cn.duli.xianer.pojo.UserLoginAuths;
import cn.duli.xianer.service.UserLoginAuthsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class UserLoginAuthsServiceImpl implements UserLoginAuthsService {


    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private TemplateEngine templateEngine;

    //这一步是获取application.properties中设置的发件人邮箱地址
    @Value("${spring.mail.username}")
    private String username;

    @Resource
    private UserLoginAuthsMapper userLoginAuthsMapper;

    @Override
    public UserLoginAuths findByIdentifier(String identifier) {
        return userLoginAuthsMapper.findByIdentifier(identifier);
    }


    @Override
    public boolean sendEmail(String email, Integer code) throws MessagingException {
        try {
            //发邮件
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 第二个参数true表示使用HTML语言来编写邮件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            //发件人邮件地址(上面获取到的，也可以直接填写,string类型)
            helper.setFrom(username);
            //收件人邮件地址
            helper.setTo(email);
            //邮件主题
            helper.setSubject("闲贰注册验证码");

            //使用模板thymeleaf
            //Context是导这个包import org.thymeleaf.context.Context;
            Context context = new Context();
            //设置传入模板的页面的参数 参数名为:code 参数随便写一个就行
            context.setVariable("code", code);
            //emailTemplate是你要发送的模板我这里用的是Thymeleaf
            String emailContent = templateEngine.process("codeEmail", context);
            //邮件正文
            helper.setText(emailContent, true);
            javaMailSender.send(mimeMessage);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public int save(UserLoginAuths userLoginAuth) {
        return userLoginAuthsMapper.save(userLoginAuth);

    }

    @Override
    public UserLoginAuths findByIdentifierAndCredential(String identifier, String credential) {
        return userLoginAuthsMapper.findByIdentifierAndCredential(identifier, credential);
    }

    @Override
    public UserLoginAuths findByUserId(Integer uId) {
        return userLoginAuthsMapper.findByUserId(uId);
    }

    @Override
    public List<UserLoginAuths> findAllByUserId(Integer uId) {
        return userLoginAuthsMapper.findAllByUserId(uId);
    }

    @Override
    public void updateCredential(UserLoginAuths userLoginAuth) {
        userLoginAuthsMapper.updateCredential(userLoginAuth);
    }

    @Override
    public boolean updateEmail(String email) throws MessagingException {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(username);
            helper.setTo(email);
            helper.setSubject("闲贰登录密码修改成功");
            Context context = new Context();
            context.setVariable("email", email);
            //emailTemplate是你要发送的模板我这里用的是Thymeleaf
            String emailContent = templateEngine.process("updateEmail", context);
            helper.setText(emailContent, true);
            javaMailSender.send(mimeMessage);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
